package io.zcoder.innoway.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.models.ResponseBase;
import io.zcoder.innoway.rest.RestClient;
import io.zcoder.innoway.widgets.EditTextCustom;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by thanh on 12/5/16.
 */

public class ForgotPasswordActivity extends BaseActivity {
    @BindView(R.id.layout_forget)
    RelativeLayout mLayoutForget;

    @BindView(R.id.layout_success)
    RelativeLayout mLayoutSuccess;

    @BindView(R.id.email)
    EditTextCustom mEditEmail;

    @OnClick(R.id.btn_reset)
    void onReset() {
        String email = mEditEmail.getText().toString();
        if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            showAlert(getString(R.string.error), getString(R.string.error_email));
            return;
        }
        RestClient.build(this, false).resetPassword(email, new Callback<ResponseBase>() {
            @Override
            public void success(ResponseBase responseBase, Response response) {
                if (responseBase.getStatus_code() != 200) {
                    showAlert(getString(R.string.error), responseBase.getMessage());
                    return;
                }
                mLayoutForget.setVisibility(View.INVISIBLE);
                mLayoutSuccess.setVisibility(View.VISIBLE);
            }

            @Override
            public void failure(RetrofitError error) {
                showAlert(getString(R.string.error), error.getMessage());
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }
}
